package cn.iocoder.yudao.module.javainfo.controller.admin.userpet;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.javainfo.controller.admin.userpet.vo.*;
import cn.iocoder.yudao.module.javainfo.dal.dataobject.userpet.UserPetDO;
import cn.iocoder.yudao.module.javainfo.service.userpet.UserPetService;

@Tag(name = "管理后台 - 用户宠物关联")
@RestController
@RequestMapping("/javainfo/user-pet")
@Validated
public class UserPetController {

    @Resource
    private UserPetService userPetService;

    @PostMapping("/create")
    @Operation(summary = "创建用户宠物关联")
    @PreAuthorize("@ss.hasPermission('javainfo:user-pet:create')")
    public CommonResult<Long> createUserPet(@Valid @RequestBody UserPetSaveReqVO createReqVO) {
        return success(userPetService.createUserPet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户宠物关联")
    @PreAuthorize("@ss.hasPermission('javainfo:user-pet:update')")
    public CommonResult<Boolean> updateUserPet(@Valid @RequestBody UserPetSaveReqVO updateReqVO) {
        userPetService.updateUserPet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户宠物关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('javainfo:user-pet:delete')")
    public CommonResult<Boolean> deleteUserPet(@RequestParam("id") Long id) {
        userPetService.deleteUserPet(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户宠物关联")
                @PreAuthorize("@ss.hasPermission('javainfo:user-pet:delete')")
    public CommonResult<Boolean> deleteUserPetList(@RequestParam("ids") List<Long> ids) {
        userPetService.deleteUserPetListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户宠物关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('javainfo:user-pet:query')")
    public CommonResult<UserPetRespVO> getUserPet(@RequestParam("id") Long id) {
        UserPetDO userPet = userPetService.getUserPet(id);
        return success(BeanUtils.toBean(userPet, UserPetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户宠物关联分页")
    @PreAuthorize("@ss.hasPermission('javainfo:user-pet:query')")
    public CommonResult<PageResult<UserPetRespVO>> getUserPetPage(@Valid UserPetPageReqVO pageReqVO) {
        PageResult<UserPetDO> pageResult = userPetService.getUserPetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户宠物关联 Excel")
    @PreAuthorize("@ss.hasPermission('javainfo:user-pet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPetExcel(@Valid UserPetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPetDO> list = userPetService.getUserPetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户宠物关联.xls", "数据", UserPetRespVO.class,
                        BeanUtils.toBean(list, UserPetRespVO.class));
    }

}